#include "bspUsart.h"
#include <string.h>











 u8 gUsart3RxBuffer[USART3_RX_BUFFER_SIZE];
 u8 gUsart3TxBuffer[USART3_TX_BUFFER_SIZE];
 
 u8 gUsart2TxBuffer[USART2_TX_BUFFER_SIZE];
 u8	gUsart2RxBuffer[USART2_RX_BUFFER_SIZE];
 
 u8 gUsart1TxBuffer[USART1_TX_BUFFER_SIZE];
 u8	gUsart1RxBuffer[USART1_RX_BUFFER_SIZE];
 
 u32 gUsart3RxSize = 0;
 u32 gUsart2RxSize = 0;
 u32 gUsart1RxSize = 0;
 
 extern char RS485_Baud_StopBit;

 bool gUsart3RxEndFlag = false;
 bool gUsart2RxEndFlag = false;
 bool gUsart1RxEndFlag = false;

static void bspUsart1DmaInit(void);
static void bspUsart1NvicInit(void);

static void bspUsart2DmaInit(void);
static void bspUsart2NvicInit(void);

static void bspUsart3DmaInit(void);
static void bspUsart3NvicInit(void);

/**
  * @brief  ���ڳ�ʼ��
  * @param  u32 ������
  * @retval void
  */
void bspUsart1Init(u32 baudrate)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
												 RCC_APB2Periph_USART1 | 
												 RCC_APB2Periph_AFIO, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	bspUsart1DmaInit();
	bspUsart1NvicInit();
	
	USART_InitStruct.USART_BaudRate = baudrate;
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStruct.USART_Parity = USART_Parity_No;
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART1, &USART_InitStruct);
	
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);

#if (!USE_USART1_DMA_RX)	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	
#endif

	USART_ClearFlag(USART1, USART_IT_IDLE);
	USART_ClearFlag(USART1, USART_IT_RXNE);
	USART_ClearFlag(USART1, USART_IT_TC);

#if (USE_USART1_DMA_TX)
	USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
#endif

#if (USE_USART1_DMA_RX)
	USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
#endif	

	USART_Cmd(USART1, ENABLE);
}

static void bspUsart1DmaInit(void)
{

#if (USE_USART1_DMA_TX || USE_USART1_DMA_RX)	
	DMA_InitTypeDef DMA_InitStruct;

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
#endif

#if (USE_USART1_DMA_TX)	
	
	DMA_DeInit(DMA1_Channel4);
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)gUsart1TxBuffer;
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStruct.DMA_BufferSize = USART1_TX_BUFFER_SIZE;
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel4, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel4, DISABLE);	
	
	DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
#endif

#if (USE_USART1_DMA_RX)
	
	DMA_DeInit(DMA1_Channel5);
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)gUsart1RxBuffer;
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStruct.DMA_BufferSize = USART1_RX_BUFFER_SIZE;
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel5, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel5, ENABLE);	
#endif	
}

static void bspUsart1NvicInit(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
#if (USE_USART1_DMA_TX)	
	NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);	
#endif	





	
	NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}
/**
  * @brief  ���ڳ�ʼ��
  * @param  u32 ������
  * @retval void
  */
void bspUsart2Init(u32 baudrate)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	bspUsart2NvicInit();
	bspUsart2DmaInit();	
	
	USART_InitStruct.USART_BaudRate = baudrate;
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStruct.USART_Parity = USART_Parity_No;
	
	if(RS485_Baud_StopBit==0x01)
		USART_InitStruct.USART_StopBits = USART_StopBits_1;
	else if(RS485_Baud_StopBit==0x02)
		USART_InitStruct.USART_StopBits = USART_StopBits_1_5;
	else if(RS485_Baud_StopBit==0x03)
		USART_InitStruct.USART_StopBits = USART_StopBits_2;
	
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART2, &USART_InitStruct);
	
	USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
	
#if (!USE_USART2_DMA_RX)	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);	
#endif

	USART_ClearFlag(USART2, USART_IT_IDLE);
	USART_ClearFlag(USART2, USART_IT_RXNE);
	USART_ClearFlag(USART2, USART_IT_TC);
	
#if (USE_USART2_DMA_TX)	
	USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
#endif

#if (USE_USART2_DMA_RX)
	USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
#endif

	USART_Cmd(USART2, ENABLE);		
}

static void bspUsart2DmaInit(void)
{	
#if (USE_USART2_DMA_TX || USE_USART2_DMA_RX)	
	DMA_InitTypeDef DMA_InitStruct;

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
#endif

#if (USE_USART2_DMA_TX)	
	
	DMA_DeInit(DMA1_Channel7);
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART2->DR;
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)gUsart2TxBuffer;
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStruct.DMA_BufferSize = USART2_TX_BUFFER_SIZE;
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel7, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel7, DISABLE);	
	
	DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE);
#endif

#if (USE_USART2_DMA_RX)
	
	DMA_DeInit(DMA1_Channel6);
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART2->DR;
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)gUsart2RxBuffer;
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStruct.DMA_BufferSize = USART2_RX_BUFFER_SIZE;
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel6, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel6, ENABLE);	
#endif	
}

static void bspUsart2NvicInit(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
#if (USE_USART2_DMA_TX)	
	NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel7_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);	
#endif	





	
	NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 5;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}
/**
  * @brief  ���ڳ�ʼ��
  * @param  u32 ������
  * @retval void
  */
void bspUsart3Init(u32 baudrate)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	bspUsart3NvicInit();
	bspUsart3DmaInit();	
	
	USART_InitStruct.USART_BaudRate = baudrate;
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStruct.USART_Parity = USART_Parity_No;
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART3, &USART_InitStruct);
	
	USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);

#if (!USE_USART3_DMA_RX)	
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);	
#endif

	USART_ClearFlag(USART3, USART_IT_IDLE);
	USART_ClearFlag(USART3, USART_IT_RXNE);
	USART_ClearFlag(USART3, USART_IT_TC);
	
#if (USE_USART3_DMA_TX)	
	USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
#endif

#if (USE_USART3_DMA_RX)
	USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
#endif

	USART_Cmd(USART3, ENABLE);
	
}

static void bspUsart3DmaInit(void)
{	
#if (USE_USART3_DMA_TX || USE_USART3_DMA_RX)	
	DMA_InitTypeDef DMA_InitStruct;

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
#endif

#if (USE_USART3_DMA_TX)	
	
	DMA_DeInit(DMA1_Channel2);
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART3->DR;
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)gUsart3TxBuffer;
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStruct.DMA_BufferSize = USART3_TX_BUFFER_SIZE;
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel2, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel2, DISABLE);	
	
	DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
#endif

#if (USE_USART3_DMA_RX)
	
	DMA_DeInit(DMA1_Channel3);
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)&USART3->DR;
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)gUsart3RxBuffer;
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStruct.DMA_BufferSize = USART3_RX_BUFFER_SIZE;
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel3, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel3, ENABLE);
#endif	
}

static void bspUsart3NvicInit(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
#if (USE_USART3_DMA_TX)	
	NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel2_IRQn; 
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
#endif
	
	NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn; 
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}

/**
	* @brief  ���ڷ�������
	* @param  const u8* ָ��Ҫ���͵ĵ�buf
	* @param	u32 Ҫ���͵����ݳ���
  * @retval void
  */
void bspUsart1Send(const char* str, u32 size)
{
#if (USE_USART1_DMA_TX)	
	
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
	
	memcpy(gUsart1TxBuffer, str, size);
	
	DMA_SetCurrDataCounter(DMA1_Channel4, size);
	
	DMA_Cmd(DMA1_Channel4, ENABLE);

#else
	u32 i=0;

	while(i < size)
	{
		USART_SendData(USART1, str[i++]);
		while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
	}
	while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);	
	
#endif	
}

/**
  * @brief  ��ȡ���ڽ��յ�������
  * @param  void** ָ�����buf
  * @retval int ���ؽ��յ����ֽ���
  */
u32 bspUsart1Receive(void** buffer)
{
	u32 size;
	
	gUsart1RxEndFlag = false;
	*buffer = &gUsart1RxBuffer;
	
#if (USE_USART1_DMA_RX)
	
	DMA_SetCurrDataCounter(DMA1_Channel5, USART1_RX_BUFFER_SIZE);
	DMA_Cmd(DMA1_Channel5, ENABLE);
	
#else
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	
	
#endif	
	
	size = gUsart1RxEndFlag;
	gUsart1RxEndFlag = 0;
	
	return size;
}


/**
  * @brief  ��ȡ�����Ƿ�֡���ݿ���
  * @param  None
  * @retval 0 �����ݿ��ã�1 �����ݿ���
  */
bool bspUsart1Available(void)
{
	return gUsart1RxEndFlag;
}

/**
	* @brief  ���ڷ�������
	* @param  const u8* ָ��Ҫ���͵ĵ�buf
	* @param	u32 Ҫ���͵����ݳ���
  * @retval void
  */
void bspUsart2Send(const char* str, u32 size)
{
#if (USE_USART2_DMA_TX)	
	
	while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
	memcpy(gUsart2TxBuffer, str, size);
	DMA_SetCurrDataCounter(DMA1_Channel7, size);
	DMA_Cmd(DMA1_Channel7, ENABLE);

#else
	u32 i=0;

	while(i < size)
	{
		USART_SendData(USART2, str[i++]);
		while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
	}
	while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);	
	
#endif	
}

/**
  * @brief  ��ȡ���ڽ��յ�������
  * @param  void** ָ�����buf
  * @retval int ���ؽ��յ����ֽ���
  */
u32 bspUsart2Receive(void** buffer)
{
	u32 size;
	
	gUsart2RxEndFlag = false;
	*buffer = &gUsart2RxBuffer;
	
#if (USE_USART2_DMA_RX)	

	DMA_SetCurrDataCounter(DMA1_Channel6, USART2_RX_BUFFER_SIZE);
	DMA_Cmd(DMA1_Channel6, ENABLE);
	
#else
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);	
	
#endif	
	
	size = gUsart2RxSize;
	gUsart2RxSize = 0;
	
	return size;
}


/**
  * @brief  ��ȡ�����Ƿ�֡���ݿ���
  * @param  None
  * @retval 0 �����ݿ��ã�1 �����ݿ���
  */
bool bspUsart2Available(void)
{
	return gUsart2RxEndFlag;
}

/**
	* @brief  ���ڷ�������
	* @param  const u8* ָ��Ҫ���͵ĵ�buf
	* @param	u32 Ҫ���͵����ݳ���
  * @retval void
  */
void bspUsart3Send(const char* str, u32 size)
{
	
#if (USE_USART3_DMA_TX)	
	
	while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
	memcpy(gUsart3TxBuffer, str, size);
	DMA_SetCurrDataCounter(DMA1_Channel2, size);
	DMA_Cmd(DMA1_Channel2, ENABLE);
	
#else
	
	u32 i=0;

	while(i < size)
	{
		USART_SendData(USART3, str[i++]);
		while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET);
	}
	while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET);	
	
#endif	
}

/**
  * @brief  ��ȡ���ڽ��յ�������
  * @param  void** ָ�����buf
  * @retval int ���ؽ��յ����ֽ���
  */
u32 bspUsart3Receive(void** buffer)
{
	u32 size;
	
	gUsart3RxEndFlag = false;
	*buffer = &gUsart3RxBuffer;
	
#if (USE_USART3_DMA_RX)			
	


	
#else
	
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);	
	
#endif	
	
	size = gUsart3RxSize;
	gUsart3RxSize = 0;
	
	return size;
}

/**
  * @brief  ��ȡ�����Ƿ�֡���ݿ���
  * @param  None
  * @retval 0 �����ݿ��ã�1 �����ݿ���
  */
bool bspUsart3Available(void)
{
	return gUsart3RxEndFlag;
}

void bspUsart3CleanData(void)
{
	memset(gUsart3RxBuffer, 0 ,gUsart3RxSize);
	gUsart3RxSize = 0;
}

void bspUsart2CleanData(void)
{
	memset(gUsart2RxBuffer, 0 ,gUsart2RxSize);
	gUsart2RxSize = 0;
}

/**
  * @brief  ����1��������û��ص�����
  * @param  void
  * @retval void
  */
__WEAK void Usart1RxCallback(void)
{
}

/**
  * @brief  ����2��������û��ص�����
  * @param  void
  * @retval void
  */
__WEAK void Usart2RxCallback(void)
{
}

/**
  * @brief  ����3��������û��ص�����
  * @param  void
  * @retval void
  */
__WEAK void Usart3RxCallback(void)
{
}

void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(USART1,USART_IT_IDLE)!=RESET)
	{
		USART1->SR;USART1->DR;

#if (USE_USART1_DMA_RX)		
		
		DMA_Cmd(DMA1_Channel5, DISABLE);
	
		gUsart1RxSize = USART1_RX_BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);
		




#else
		USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);	
#endif		
		
		gUsart1RxEndFlag = true;
		
		Usart1RxCallback();
	}
#if (!USE_USART1_DMA_RX)
	
	else if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
	{
		gUsart1RxBuffer[gUsart1RxSize++] = USART_ReceiveData(USART1);
		if(gUsart1RxSize >= USART1_RX_BUFFER_SIZE) gUsart1RxSize = 0;
	}
	
#endif	
}

void USART2_IRQHandler(void)
{
	if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
	{
		USART2->SR;USART2->DR;
		
#if (USE_USART2_DMA_RX)		
		
		DMA_Cmd(DMA1_Channel6, DISABLE);
		
		gUsart2RxSize = USART2_RX_BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Channel6);
		



		
#else
		USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);	
#endif
		
		gUsart2RxEndFlag = true;
		
		Usart2RxCallback();
		
	}
#if (!USE_USART2_DMA_RX)
	
	else if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{
		gUsart2RxBuffer[gUsart2RxSize++] = USART_ReceiveData(USART2);
		if(gUsart2RxSize >= USART2_RX_BUFFER_SIZE) gUsart2RxSize = 0;
	}
	
#endif	
}

void USART3_IRQHandler(void)
{
	if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
	{
		USART3->SR;USART3->DR;
#if (USE_USART3_DMA_RX)		
		DMA_Cmd(DMA1_Channel3, DISABLE);
		
		gUsart3RxSize = USART3_RX_BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Channel3);

		DMA_SetCurrDataCounter(DMA1_Channel3,USART3_RX_BUFFER_SIZE);
		
		DMA_Cmd(DMA1_Channel3,ENABLE);	
#else
		USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);	
#endif		
		gUsart3RxEndFlag = true;
		
		Usart3RxCallback();
	}
	
#if (!USE_USART3_DMA_RX)
	
	else if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
	{
		gUsart3RxBuffer[gUsart3RxSize++] = USART_ReceiveData(USART3);
		if(gUsart3RxSize >= USART3_RX_BUFFER_SIZE) gUsart3RxSize = 0;
	}
	
#endif
}

#if (USE_USART3_DMA_TX)
__WEAK void Usart3TxCallback(void)
{
	
}

void DMA1_Channel2_IRQHandler(void)
{
	if(DMA_GetITStatus(DMA1_FLAG_TC2)!=RESET)
	{
		DMA_Cmd(DMA1_Channel2, DISABLE);
		DMA_ClearITPendingBit(DMA1_FLAG_TC2);
		
		Usart3TxCallback();
	}	
}
#endif

#if (USE_USART2_DMA_TX)
__WEAK void Usart2TxCallback(void)
{
}

void DMA1_Channel7_IRQHandler(void)
{
	if(DMA_GetITStatus(DMA1_FLAG_TC7)!=RESET)
	{
		DMA_Cmd(DMA1_Channel7, DISABLE);
		DMA_ClearITPendingBit(DMA1_FLAG_TC7);
		
		Usart2TxCallback();
	}	
}
#endif

#if (USE_USART1_DMA_TX)
__WEAK void Usart1TxCallback(void)
{
	
}

void DMA1_Channel4_IRQHandler(void)
{
	if(DMA_GetITStatus(DMA1_FLAG_TC4)!=RESET)
	{
		DMA_Cmd(DMA1_Channel4, DISABLE);
		DMA_ClearITPendingBit(DMA1_FLAG_TC4);
		
		Usart1TxCallback();
	}
}
#endif
